package bmp.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Properties;

public class PropertiesUtil {
	private Properties prop;
	private String topDir;
	private String outputTopFolder;
	private String outputFolder;
	private String username;
	private int number;
	private String lastRunDate;

	public PropertiesUtil() {
		prop = new Properties();
		InputStream input = null;

		try {

			input = new FileInputStream("config.conf");

			// load a properties file
			prop.load(input);

			// get the property value
			topDir = prop.getProperty("common.topDir");
			outputTopFolder = prop.getProperty("common.outputTopFolder");
			username = prop.getProperty("personal.username");
			if ("".equals(prop.getProperty("personal.number"))) {
				number = 0;
			} else {
				number = Integer.valueOf(prop.getProperty("personal.number"));
			}
			lastRunDate = prop.getProperty("personal.lastRunDate");

			Calendar now = new GregorianCalendar(Calendar.getInstance().get(
					Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH),
					Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String strNow = sdf.format(now.getTime());
			
			if (!"".equals(lastRunDate)) {
				Date dateLstRun = sdf.parse(lastRunDate);
				Calendar lstRun = new GregorianCalendar();
				lstRun.setTime(dateLstRun);
				if (lstRun.compareTo(now) < 0) {
					lastRunDate = strNow;
					number = 1;
				} else {
					number++;
				}
			} else {
				lastRunDate = strNow;
				number = 1;
			}
			
			outputFolder = prop.getProperty("common.outputFolder");
			outputFolder = outputFolder.replace("yyyyMMdd", this.lastRunDate);
			outputFolder = outputFolder.replace("username", this.username);
			outputFolder = outputFolder.replace("number", String.valueOf(this.number));

		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void update() {
		OutputStream output = null;
		try {
			output = new FileOutputStream("config.conf");
			// set the properties value
			prop.setProperty("personal.number", String.valueOf(this.number));
			prop.setProperty("personal.lastRunDate", this.lastRunDate);

			// save properties to project root folder
			prop.store(output, null);

		} catch (IOException io) {
			io.printStackTrace();
		} finally {
			if (output != null) {
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public String getTopDir() {
		return topDir;
	}

	public void setTopDir(String topDir) {
		this.topDir = topDir;
	}

	public String getOutputTopFolder() {
		return outputTopFolder;
	}

	public void setOutputTopFolder(String outputTopFolder) {
		this.outputTopFolder = outputTopFolder;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getLastRunDate() {
		return lastRunDate;
	}

	public void setLastRunDate(String lastRunDate) {
		this.lastRunDate = lastRunDate;
	}

	public String getOutputFolder() {
		return outputFolder;
	}

	public void setOutputFolder(String outputFolder) {
		this.outputFolder = outputFolder;
	}
	
}
